package com.youlai.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.system.common.result.Result;
import com.youlai.system.mapper.StockProviderMapper;
import com.youlai.system.mapper.StockPurchaseItemMapper;
import com.youlai.system.mapper.StockPurchaseMapper;
import com.youlai.system.model.entity.StockProvider;
import com.youlai.system.model.entity.StockPurchase;
import com.youlai.system.model.entity.StockPurchaseItem;
import com.youlai.system.model.entity.Stock_emdicines;
import com.youlai.system.security.util.SecurityUtils;
import com.youlai.system.security.util.SnowflakeDistributeId;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @作业 chen
 * @时间 2024-09-05 14:13
 */
@RestController
@RequestMapping("/api/v1/StockPurchase")
@RequiredArgsConstructor
@Slf4j
public class StockPurchaseController {
    private final StockPurchaseMapper stockPurchaseMapper;
    private final StockProviderMapper stockProviderMapper;
    private final StockPurchaseItemMapper stockPurchaseItemMapper;

    @RequestMapping("/list")
    public Result list(@RequestBody StockPurchase stockPurchase) {
        Page page = new Page(stockPurchase.getCurrentPage4(), stockPurchase.getPageSize4());
        LambdaQueryWrapper<StockPurchase> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(stockPurchase.getProviderId() != null, StockPurchase::getProviderId, stockPurchase.getProviderId());
        queryWrapper.like(!stockPurchase.getApplyUserName().isEmpty(), StockPurchase::getApplyUserName, stockPurchase.getApplyUserName());
        queryWrapper.eq(!stockPurchase.getStatus().isEmpty(), StockPurchase::getStatus, stockPurchase.getStatus());
        Page page1 = stockPurchaseMapper.selectPage(page, queryWrapper);
        return Result.success(page1);
    }

    @RequestMapping("/seletechangshang")
    public Result seletechangshang() {
        LambdaQueryWrapper<StockProvider> queryWrapper = new LambdaQueryWrapper<>();
        return Result.success(stockProviderMapper.selectList(queryWrapper));
    }


    @RequestMapping("/Snowflakealgorithm")
    public Result<Long> Snowflakealgorithm() {
        SnowflakeDistributeId idWorker = new SnowflakeDistributeId(0, 0);
        long newId = idWorker.nextId();
        System.out.println("新id：" + newId);
        return Result.success(newId);
    }

    //添加采购入库单
    @PostMapping("/addPurchaseInStock")
    @Transactional
    public Result addPurchaseInStock(@RequestBody StockPurchase stockPurchase) {
        try {
            System.out.println(stockPurchase);
            SnowflakeDistributeId idWorker = new SnowflakeDistributeId(0, 0);
            stockPurchase.setStatus("2");
            stockPurchase.setApplyUserId(SecurityUtils.getUserId());
            stockPurchase.setApplyUserName(SecurityUtils.getUsername());
            stockPurchase.setCreateTime(new Date());
            stockPurchase.setCreateBy(SecurityUtils.getUsername());
            stockPurchaseMapper.insert(stockPurchase);
            stockPurchase.getPurchaseItemList().forEach(item -> {
                item.setItemId(String.valueOf(idWorker.nextId()));
                item.setPurchaseId(stockPurchase.getPurchaseId());
                stockPurchaseItemMapper.insert(item);
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Result.success();
    }

    //暂存采购入库单
    @PostMapping("/savePurchaseInStock")
    @Transactional
    public Result savePurchaseInStock(@RequestBody StockPurchase stockPurchase) {
        try {
            System.out.println(stockPurchase);
            SnowflakeDistributeId idWorker = new SnowflakeDistributeId(0, 0);
            stockPurchase.setStatus("1");
            stockPurchase.setApplyUserId(SecurityUtils.getUserId());
            stockPurchase.setApplyUserName(SecurityUtils.getUsername());
            stockPurchase.setCreateTime(new Date());
            stockPurchase.setCreateBy(SecurityUtils.getUsername());
            stockPurchaseMapper.insert(stockPurchase);
            stockPurchase.getPurchaseItemList().forEach(item -> {
                item.setItemId(String.valueOf(idWorker.nextId()));
                item.setPurchaseId(stockPurchase.getPurchaseId());
                stockPurchaseItemMapper.insert(item);
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return Result.success();
    }
}
